/**
 * 初始化列表
 * @param {type} param
 */
var $table = $('#employeeList').DataTable({
//    "processing": true,
//    "serverSide": true,
    "ajax": "employee/getData",
    "columns": [
        {"data": "loginName"},
        {"data": "name"},
        {"data": "mobile"},
        {"data": "departmentName"},
        {"data": "enabled"},
        {"data": "registerTime"},
        {"data": "id"}
    ],
    "aoColumnDefs": [{
            "targets": 0,
            "mRender": function (data, type, row) {
                return '<a href="employee/index/' + row.id + '" target="_blank">' + data + '</a>';
            }
        }, {
            "targets": 2,
            "mRender": function (data, type, row) {
                return data ? data : '-';
            }
        }, {
            "targets": 3,
            "mRender": function (data, type, row) {
                return data === null ? "-" : data;
            }
        }, {
            "targets": 4,
            "searchable": false,
            "mRender": function (data, type, row) {
                var status = data
                    ? '<i class="fa fa-check text-success"></i>正常'
                    : '<i class="fa fa-lock text-danger mr4"></i>冻结';
                return status;
            }
        }, {
            "targets": -2,
            "searchable": false,
            "mRender": function (data, type, row) {
                return $.format.date(new Date(data), "yyyy/MM/dd");
            }
        }, {
            "targets": -1,
            "bSortable": false,
            "searchable": false,
            //"className": "text-center",
            "mRender": function (data, type, row) {
                var operations = $("#operationHTML").html().replace(/#id/g, row.id);
                if (!row.enabled) {
                    operations = operations.replace("glyphicons-lock", "glyphicons-unlock").replace("冻结", "激活");
                }
                return operations;
            }
        }],
    "order": [[4, "desc"]],
    "oLanguage": chinese,
    "iDisplayLength": 10,
    "aLengthMenu": [
        [5, 10, 25, 50, -1],
        [5, 10, 25, 50, "全部"]
    ],
    "sDom": '<"dt-panelmenu clearfix"l<"pull-right"Tf>r>t<"dt-panelfooter clearfix"ip>',
    "oTableTools": {
        "aButtons": [{
                "sExtends": "copy",
                "sButtonText": "复制",
                "fnComplete": function (nButton, oConfig, oFlash, sFlash) {
                    alert('列表内容已复制到剪贴板');
                }
            }, {
                "sExtends": "xls",
                "sButtonText": "导出为Excel"
            }
//            , {
//                "sExtends": "csv",
//                "sButtonText": "CSV"
//            }, {
//                "sExtends": "pdf",
//                "sButtonText": "PDF"
//            }
        ],
        "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
    }
});
// 添加行号
//$table.on('order.dt search.dt', function () {
//    $table.column(0, {search: 'applied', order: 'applied'}).nodes().each(function (cell, i) {
//        cell.innerHTML = i + 1;
//    });
//}).draw();


$("#employeeList").on("click", ".reset-pwd-btn", function (e) {
    e.preventDefault();
    e.stopPropagation();
    var id = $(this).data("id");
    sys.confirm("确定重置员工密码？", function () {
        $.get("employee/resetPassword/" + id, function (res) {
            if (res.success) {
                alert("员工密码已重置");
                location.reload();
            } else {
                alert("重置员工密码失败");
                sys.closeModal();
            }
        });
    });
});


/**
 * 更改账号状态
 * @param {type} employeeId
 * @returns {undefined}
 */
function changeStatus(employeeId) {
    if (confirm('确定要更新员工状态？')) {
        $.get("employee/changeStatus/" + employeeId, function (res) {
            if (res.success) {
                alert(res.message);
                location.reload();
            } else {
                alert("员工状态更新失败");
            }
        });
    }
}